Skip to content

Conversation

decsny
Copy link
Member

@decsny decsny commented Jul 21, 2025

When building with CONFIG_DEBUG=y, there is a build warning about the name being truncated due to integer size is larger than MAX_NAME_LEN. So fix with modulo operator to be clear to the compiler what we expect.

@decsny decsny added the Trivial Changes that can be reviewed by anyone, i.e. doc changes, minor build system tweaks, etc. label Jul 21, 2025
@zephyrbot zephyrbot added area: Sockets Networking sockets area: HTTP HTTP client/server support area: Samples Samples area: Networking size: XS A PR changing only a single line of code labels Jul 21, 2025
char name[MAX_NAME_LEN];

snprintk(name, sizeof(name), "ws[%d]", slot);
snprintk(name, sizeof(name), "ws[%d]", slot % (10 * MAX_NAME_LEN));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not look right. The idea is to print the slot value here. If the name variable is too small, let's increase the size of it instead, so something like
MAX_NAME_LEN sizeof("ws[xxxx]")

Copy link
Member Author

@decsny decsny Jul 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this is not right what I have here, but I think what you suggested also won't help, the issue appears to be that the compiler knows that the max integer value is 10 digits, so the max size would theoretically be sizeof("ws[xxxxxxxxxx]"), is that what you think it should be changed to ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed the same idea as you said but did sizeof("ws[]") + 10

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this sizeof("ws[xxxxxxxxxx]") is what I was suggesting, so just adding enough x to silence the warning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having +10 certainly works, but it is now a magical constant and not immediately obvious why it is there. Therefore we used the "template" way in other part of the code and I suggest we do that here too.

@decsny decsny force-pushed the fix/http_server_ws_name_warning branch from d81befc to 1e995c6 Compare July 22, 2025 21:39
When building with CONFIG_DEBUG=y, there is a build warning about the
name being truncated due to integer size is larger than MAX_NAME_LEN.
So fix MAX_NAME_LEN to be large enough to handle 10 digit integer, which
is maximum.

Signed-off-by: Declan Snyder <[email protected]>
@decsny decsny force-pushed the fix/http_server_ws_name_warning branch from 1e995c6 to 0c58043 Compare August 5, 2025 14:51
@decsny decsny requested a review from jukkar August 5, 2025 14:51
@zephyrbot zephyrbot requested a review from matt-rodgers August 5, 2025 14:51
Copy link

sonarqubecloud bot commented Aug 5, 2025


if (IS_ENABLED(CONFIG_THREAD_NAME)) {
#define MAX_NAME_LEN sizeof("ws[xx]")
#define MAX_NAME_LEN (sizeof("ws[xxxxxxxxxx]"))
Copy link
Member

@cfriedt cfriedt Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@decsny - I don't want to be too nit-picky, so this isn't blocking by any means, but I think MAX_NAME_LEN should probably be defined in terms of CONFIG_THREAD_MAX_NAME_LEN. If you can respin, sure, but otherwise, I think we can probably merge anyway.

Below, N is the minimum expected number of digits to use for slots

Suggested change
#define MAX_NAME_LEN (sizeof("ws[xxxxxxxxxx]"))
BUILD_ASSERT(CONFIG_THREAD_MAX_NAME_LEN > (sizeof("ws[]") + N), "CONFIG_THREAD_MAX_NAME_LEN is too small");
#define MAX_NAME_LEN (CONFIG_THREAD_MAX_NAME_LEN - sizeof("ws[]"))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defer to @jukkar who had this opinion for how it is now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that we go with what we have now, we have used this pattern in other part of networking code too. If we want to have build asserts, we could introduce that to all places that have the name used.


if (IS_ENABLED(CONFIG_THREAD_NAME)) {
#define MAX_NAME_LEN sizeof("ws[xx]")
#define MAX_NAME_LEN (sizeof("ws[xxxxxxxxxx]"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that we go with what we have now, we have used this pattern in other part of networking code too. If we want to have build asserts, we could introduce that to all places that have the name used.

@jhedberg jhedberg merged commit c96d891 into zephyrproject-rtos:main Aug 6, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: HTTP HTTP client/server support area: Networking area: Samples Samples area: Sockets Networking sockets size: XS A PR changing only a single line of code Trivial Changes that can be reviewed by anyone, i.e. doc changes, minor build system tweaks, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants